package com.ims.core.doc.service.impl;

import java.util.List;

import com.baiwang.open.entity.ErrorResponse;
import com.baiwang.open.entity.response.*;
import com.baiwang.open.entity.response.node.*;
import com.ims.common.core.domain.model.LoginUser;
import com.ims.common.core.redis.RedisCache;
import com.ims.common.exception.ServiceException;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.DictUtils;
import com.ims.common.utils.SecurityUtils;
import com.ims.common.utils.uuid.IdUtils;
import com.ims.core.doc.domain.*;
import com.ims.core.doc.service.ITInDeductionStatApplyResultService;
import com.ims.core.doc.service.ITInDeductionStatApplyService;
import com.ims.core.enums.*;
import com.ims.manager.ImsAsyncManager;
import com.ims.manager.factory.ImsAsyncFactory;
import com.ims.system.domain.TSysSite;
import com.ims.system.service.ITSysSiteService;
import com.ims.task.DeductionStatTask;
import com.ims.util.BaiwangUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.doc.mapper.TInDeductionStatMapper;
import com.ims.core.doc.service.ITInDeductionStatService;

import javax.sql.rowset.serial.SerialException;

/**
 * 统计表Service业务层处理
 *
 * @author IMS
 * @date 2024-12-11
 */
@Service
public class TInDeductionStatServiceImpl implements ITInDeductionStatService
{
    @Autowired
    private RedisCache redisCache;

    //日志
    private static final Logger logger = LoggerFactory.getLogger(TInDeductionStatServiceImpl.class);

    @Autowired
    private TInDeductionStatMapper tInDeductionStatMapper;

    @Autowired
    private ITSysSiteService tSysSiteService;

    @Autowired
    private ITInDeductionStatApplyService tInDeductionStatApplyService;

    @Autowired
    private ITInDeductionStatApplyResultService tInDeductionStatApplyResultService;

    /**
     * 查询统计表
     *
     * @param id 统计表主键
     * @return 统计表
     */
    @Override
    public TInDeductionStat selectTInDeductionStatById(Long id)
    {
        return tInDeductionStatMapper.selectTInDeductionStatById(id);
    }

    /**
     * 查询当前统计表
     *
     */
    @Override
    public TInDeductionStat selectCurrentDeductionstat()
    {
        String period = DateUtils.getCurrentMonth();
        TInDeductionStat tInDeductionStat = tInDeductionStatMapper.selectTInDeductionStatByPeriod(period);
        if(tInDeductionStat == null){
            tInDeductionStat = autoCreateInDeductionStatForThisPeriod();
        }
        // 同步统计表状态
        syncDeductionStatApplyResult(tInDeductionStat.getPeriod(), TaskType.STATISTICS_TABLE_APPLICATION.getCode(), tInDeductionStat.getTaxNo());
        tInDeductionStat = tInDeductionStatMapper.selectTInDeductionStatByPeriod(period);
        return tInDeductionStat;
    }

    /**
     * 查询统计表
     *
     * @param statId 统计表ID
     * @return 统计表
     */
    @Override
    public TInDeductionStat selectTInDeductionStatByStatId(String statId)
    {
        return tInDeductionStatMapper.selectTInDeductionStatByStatId(statId);
    }

    /**
     * 查询统计表
     *
     * @param period 税期
     * @return 统计表
     */
    @Override
    public TInDeductionStat selectTInDeductionStatByPeriod(String period)
    {
        return tInDeductionStatMapper.selectTInDeductionStatByPeriod(period);
    }

    /**
     * 查询统计表列表
     *
     * @param tInDeductionStat 统计表
     * @return 统计表
     */
    @Override
    public List<TInDeductionStat> selectTInDeductionStatList(TInDeductionStat tInDeductionStat)
    {
        return tInDeductionStatMapper.selectTInDeductionStatList(tInDeductionStat);
    }

    /**
     * 新增统计表
     *
     * @param tInDeductionStat 统计表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTInDeductionStat(TInDeductionStat tInDeductionStat)
    {
        tInDeductionStat.setStatId(IdUtils.simpleUUID());
        tInDeductionStat.setCreateTime(DateUtils.getNowDate());
        int rows = tInDeductionStatMapper.insertTInDeductionStat(tInDeductionStat);
        insertTInDeductionStatEntry(tInDeductionStat);
        return rows;
    }

    /**
     * 修改统计表
     *
     * @param tInDeductionStat 统计表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInDeductionStat(TInDeductionStat tInDeductionStat)
    {
        tInDeductionStat.setUpdateTime(DateUtils.getNowDate());
        tInDeductionStatMapper.deleteTInDeductionStatEntryByStatId(tInDeductionStat.getStatId());
        insertTInDeductionStatEntry(tInDeductionStat);
        return tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
    }
    /**
     * 新增抵扣统计明细信息
     *
     * @param tInDeductionStat 统计表对象
     */
    public void insertTInDeductionStatEntry(TInDeductionStat tInDeductionStat)
    {
        List<TInDeductionStatEntry> tInDeductionStatEntryList = tInDeductionStat.getTInDeductionStatEntryList();
        String statId = tInDeductionStat.getStatId();
        if (StringUtils.isNotNull(tInDeductionStatEntryList))
        {
            List<TInDeductionStatEntry> list = new ArrayList<TInDeductionStatEntry>();
            for (TInDeductionStatEntry tInDeductionStatEntry : tInDeductionStatEntryList)
            {
                tInDeductionStatEntry.setEntryId(IdUtils.simpleUUID());
                tInDeductionStatEntry.setStatId(statId);
                list.add(tInDeductionStatEntry);
            }
            if (list.size() > 0)
            {
                tInDeductionStatMapper.batchTInDeductionStatEntry(list);
            }
        }
    }

    /**
     *
     */
    @Override
    public void autoCreateInDeductionStatForNextPeriod() {
        // 获取默认公司
        TSysSite tSysSite = tSysSiteService.getCurrentSite();
        // 下个税期
        String period = DateUtils.getNextMonth();
        // 查询该税期的统计表是否存在
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByPeriod(period);
        // 不存在则新建一个
        if(tInDeductionStat == null){
            tInDeductionStat = new TInDeductionStat();
            tInDeductionStat.setCustId(tSysSite.getCustId());
            tInDeductionStat.setSiteCode(tSysSite.getSiteCode());
            tInDeductionStat.setSiteName(tSysSite.getSiteName());
            tInDeductionStat.setTaxNo(tSysSite.getTaxNumber());
            tInDeductionStat.setPeriod(period);
            // 1	待申请统计表
            tInDeductionStat.setStatFlowStatus(StatFlowStatus.DAI_SHEN_QING.getCode());
            tInDeductionStat.setStatFlowStatusName(StatFlowStatus.DAI_SHEN_QING.getName());
            //
            tInDeductionStat.setCreateBy("自动生成");
            insertTInDeductionStat(tInDeductionStat);
        }
        // FIXME 是否自动申请生成
        if(tInDeductionStat.getStatFlowStatus().equals(1)){
            // 调用百望统计表生成申请接口
            // ImsAsyncManager.me().execute(ImsAsyncFactory.deductionStatApplyFromBaiwang(tInDeductionStat.getStatId()));
        }
    }

    public TInDeductionStat autoCreateInDeductionStatForThisPeriod() {
        // 获取默认公司
        TSysSite tSysSite = tSysSiteService.getCurrentSite();
        // 下个税期
        String period = DateUtils.getCurrentMonth();
        // 查询该税期的统计表是否存在
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByPeriod(period);
        // 不存在则新建一个
        if(tInDeductionStat == null){
            tInDeductionStat = new TInDeductionStat();
            tInDeductionStat.setCustId(tSysSite.getCustId());
            tInDeductionStat.setSiteCode(tSysSite.getSiteCode());
            tInDeductionStat.setSiteName(tSysSite.getSiteName());
            tInDeductionStat.setTaxNo(tSysSite.getTaxNumber());
            tInDeductionStat.setPeriod(period);
            // 1	待申请统计表
            tInDeductionStat.setStatFlowStatus(StatFlowStatus.DAI_SHEN_QING.getCode());
            tInDeductionStat.setStatFlowStatusName(StatFlowStatus.DAI_SHEN_QING.getName());
            //
            tInDeductionStat.setCreateBy("自动生成");
            insertTInDeductionStat(tInDeductionStat);
        }

        // FIXME 是否自动申请生成
        if(tInDeductionStat.getStatFlowStatus().equals(1)){
            // 调用百望统计表生成申请接口
            // ImsAsyncManager.me().execute(ImsAsyncFactory.deductionStatApplyFromBaiwang(tInDeductionStat.getStatId()));
        }
        return tInDeductionStat;
    }

    /**
     * 统计表生成申请
     * @param statId 统计表ID
     */
    @Override
    public void applyTInDeductionStat(String statId) {
        // 操作人
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 查询统计表
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByStatId(statId);
        if(!tInDeductionStat.getStatFlowStatus().equals(StatFlowStatus.DAI_SHEN_QING.getCode())){
            throw new ServiceException("统计表已申请");
        }
        // 新建一个申请记录
        TInDeductionStatApply tInDeductionStatApply = new TInDeductionStatApply();
        BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatApply);
        tInDeductionStatApply.setCreateBy(loginUser.getUsername());
        tInDeductionStatApply.setApplyType("0");
        tInDeductionStatApply.setApplyTypeName("统计表生成");
        tInDeductionStatApply.setTaskType(TaskType.STATISTICS_TABLE_APPLICATION.getCode());
        tInDeductionStatApply.setTaskTypeName(TaskType.STATISTICS_TABLE_APPLICATION.getName());
        tInDeductionStatApplyService.insertTInDeductionStatApply(tInDeductionStatApply);

        // 调用百望统计表申请接口
        InputDeductionStatapplyResponse inputDeductionStatapplyResponse = BaiwangUtils.deductionStatApply(tInDeductionStat.getPeriod(), TaskType.STATISTICS_TABLE_APPLICATION.getCode(), tInDeductionStat.getTaxNo());
        // 返回失败
        if(!inputDeductionStatapplyResponse.getSuccess()){
            logger.error("统计表生成申请接口异常：" + inputDeductionStatapplyResponse.getErrorResponse().getMessage());
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("false");
            // 具体的错误码
            ErrorResponse errorResponse = inputDeductionStatapplyResponse.getErrorResponse();
            tInDeductionStatApply.setCode(errorResponse.getCode());
            tInDeductionStatApply.setMessage(errorResponse.getMessage());
            tInDeductionStatApply.setUpdateBy("接口返回异常");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改申请记录的结果
            tInDeductionStat.setSuccess("false");
            tInDeductionStat.setCode(errorResponse.getCode());
            tInDeductionStat.setMessage(errorResponse.getMessage());
            tInDeductionStat.setUpdateBy("生成异常");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
        }
        // 返回成功
        else {
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("true");
            tInDeductionStatApply.setUpdateBy("接口返回成功");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改统计表
            tInDeductionStat.setLastApplyTime(DateUtils.getNowDate());
            // 处理状态改为申请中，等待半小时后的查询结果
            tInDeductionStat.setStatFlowStatus(StatFlowStatus.SHEN_QING_ZHONG.getCode());
            tInDeductionStat.setStatFlowStatusName(StatFlowStatus.SHEN_QING_ZHONG.getName());
            tInDeductionStat.setUpdateBy("生成申请成功");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);

            // redis 插入 查询任务
            redisCache.setCacheObject(DeductionStatTask.STAT_APPLY_RESULT_TASK_KEY, tInDeductionStat.getStatId());
            // 设置任务的执行时间（半小时）
            redisCache.setCacheObject(DeductionStatTask.STAT_APPLY_RESULT_TASK_TTL_KEY, tInDeductionStat.getStatId(), 30, TimeUnit.MINUTES);
        }
    }

    /**
     * 统计表生成撤销
     * @param statId
     */
    @Override
    public void applyTInDeductionStatCancel(String statId) {
        // 操作人
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 查询统计表
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByStatId(statId);
        if(tInDeductionStat.getStatFlowStatus().equals(StatFlowStatus.DAI_SHEN_QING.getCode())){
            throw new ServiceException("统计表尚未提交申请");
        }
        // 新建一个申请记录
        TInDeductionStatApply tInDeductionStatApply = new TInDeductionStatApply();
        BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatApply);
        tInDeductionStatApply.setCreateBy(loginUser.getUsername());
        tInDeductionStatApply.setApplyType("0");
        tInDeductionStatApply.setApplyTypeName("统计表生成");
        tInDeductionStatApply.setTaskType(TaskType.STATISTICS_TABLE_CANCELLATION.getCode());
        tInDeductionStatApply.setTaskTypeName(TaskType.STATISTICS_TABLE_CANCELLATION.getName());
        tInDeductionStatApplyService.insertTInDeductionStatApply(tInDeductionStatApply);

        // 调用百望统计表撤销申请接口
        InputDeductionStatapplyResponse inputDeductionStatapplyResponse = BaiwangUtils.deductionStatApply(tInDeductionStat.getPeriod(), TaskType.STATISTICS_TABLE_CANCELLATION.getCode(), tInDeductionStat.getTaxNo());
        // 返回失败
        if(!inputDeductionStatapplyResponse.getSuccess()){
            logger.error("统计表撤销申请接口异常：" + inputDeductionStatapplyResponse.getErrorResponse().getMessage());
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("false");
            // 具体的错误码
            ErrorResponse errorResponse = inputDeductionStatapplyResponse.getErrorResponse();
            tInDeductionStatApply.setCode(errorResponse.getCode());
            tInDeductionStatApply.setMessage(errorResponse.getMessage());
            tInDeductionStatApply.setUpdateBy("接口返回异常");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改申请记录的结果
            tInDeductionStat.setSuccess("false");
            tInDeductionStat.setCode(errorResponse.getCode());
            tInDeductionStat.setMessage(errorResponse.getMessage());
            tInDeductionStat.setUpdateBy("撤销异常");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
        }
        // 返回成功
        else {
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("true");
            tInDeductionStatApply.setUpdateBy("接口返回成功");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改统计表
            tInDeductionStat.setLastApplyTime(DateUtils.getNowDate());
            // 处理状态改为确认中，等待半小时后的查询结果
            tInDeductionStat.setStatFlowStatus(StatFlowStatus.SHEN_QING_ZHONG.getCode());
            tInDeductionStat.setStatFlowStatusName(StatFlowStatus.SHEN_QING_ZHONG.getName());
            tInDeductionStat.setUpdateBy("撤销申请成功");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
            // redis 插入 查询任务
            redisCache.setCacheObject(DeductionStatTask.STAT_APPLY_RESULT_TASK_KEY, tInDeductionStat.getStatId());
            // 设置任务的执行时间（半小时）
            redisCache.setCacheObject(DeductionStatTask.STAT_APPLY_RESULT_TASK_TTL_KEY, tInDeductionStat.getStatId(), 30, TimeUnit.MINUTES);
        }

    }

    /**
     * 统计表签名确认申请
     * @param statId
     */
    @Override
    public void applyConfirmTInDeductionStat(String statId) {
        // 操作人
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 查询统计表
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByStatId(statId);
//        if(tInDeductionStat.getStatFlowStatus().equals(StatFlowStatus.YI_QING_ZHONG.getCode())){
//            throw new ServiceException("统计表尚未提交申请");
//        }
        // 新建一个申请记录
        TInDeductionStatApply tInDeductionStatApply = new TInDeductionStatApply();
        BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatApply);
        tInDeductionStatApply.setCreateBy(loginUser.getUsername());
        tInDeductionStatApply.setApplyType("1");
        tInDeductionStatApply.setApplyTypeName("确认签名");
        tInDeductionStatApply.setTaskType(ConfirmTaskType.APPLICATION.getCode());
        tInDeductionStatApply.setTaskTypeName(ConfirmTaskType.CANCELLATION.getName());
        tInDeductionStatApplyService.insertTInDeductionStatApply(tInDeductionStatApply);

        // 调用百望统计表确认申请接口
        InputDeductionStatconfirmorcancelResponse inputDeductionStatconfirmorcancelResponse = BaiwangUtils.deductionStatconfirmOrCancel(tInDeductionStat.getPeriod(), TaskType.STATISTICS_TABLE_CANCELLATION.getCode(), tInDeductionStat.getTaxNo());
        // 返回失败
        if(!inputDeductionStatconfirmorcancelResponse.getSuccess()){
            logger.error("统计表签名确认申请接口异常：" + inputDeductionStatconfirmorcancelResponse.getErrorResponse().getMessage());
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("false");
            // 具体的错误码
            ErrorResponse errorResponse = inputDeductionStatconfirmorcancelResponse.getErrorResponse();
            tInDeductionStatApply.setCode(errorResponse.getCode());
            tInDeductionStatApply.setMessage(errorResponse.getMessage());
            tInDeductionStatApply.setUpdateBy("接口返回异常");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改申请记录的结果
            tInDeductionStat.setSuccess("false");
            tInDeductionStat.setCode(errorResponse.getCode());
            tInDeductionStat.setMessage(errorResponse.getMessage());
            tInDeductionStat.setUpdateBy("确认签名异常");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
        }
        // 返回成功
        else {
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("true");
            tInDeductionStatApply.setUpdateBy("接口返回成功");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改统计表
            tInDeductionStat.setLastApplyTime(DateUtils.getNowDate());
            // 处理状态改为确认中，等待半小时后的查询结果
            tInDeductionStat.setStatFlowStatus(StatFlowStatus.QUE_REN_ZHONG.getCode());
            tInDeductionStat.setStatFlowStatusName(StatFlowStatus.QUE_REN_ZHONG.getName());
            tInDeductionStat.setUpdateBy("签名确认申请成功");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
            // redis 插入 查询任务
            redisCache.setCacheObject(DeductionStatTask.STAT_CONFIRM_APPLY_RESULT_TASK_KEY, tInDeductionStatApply.getApplyId());
            // 设置任务的执行时间（半小时）
            redisCache.setCacheObject(DeductionStatTask.STAT_CONFIRM_APPLY_RESULT_TASK_TTL_KEY, tInDeductionStatApply.getApplyId(), 30, TimeUnit.MINUTES);
        }

    }

    /**
     * 统计表签名确认取消
     * @param statId
     */
    @Override
    public void applyConfirmTInDeductionStatCancel(String statId) {
// 操作人
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 查询统计表
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByStatId(statId);
//        if(tInDeductionStat.getStatFlowStatus().equals(StatFlowStatus.YI_QING_ZHONG.getCode())){
//            throw new ServiceException("统计表尚未提交申请");
//        }
        // 新建一个申请记录
        TInDeductionStatApply tInDeductionStatApply = new TInDeductionStatApply();
        BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatApply);
        tInDeductionStatApply.setCreateBy(loginUser.getUsername());
        tInDeductionStatApply.setApplyType("1");
        tInDeductionStatApply.setApplyTypeName("确认签名");
        tInDeductionStatApply.setTaskType(ConfirmTaskType.CANCELLATION.getCode());
        tInDeductionStatApply.setTaskTypeName(ConfirmTaskType.CANCELLATION.getName());
        tInDeductionStatApplyService.insertTInDeductionStatApply(tInDeductionStatApply);

        // 调用百望统计表确认申请接口
        InputDeductionStatconfirmorcancelResponse inputDeductionStatconfirmorcancelResponse = BaiwangUtils.deductionStatconfirmOrCancel(tInDeductionStat.getPeriod(), TaskType.STATISTICS_TABLE_CANCELLATION.getCode(), tInDeductionStat.getTaxNo());
        // 返回失败
        if(!inputDeductionStatconfirmorcancelResponse.getSuccess()){
            logger.error("统计表取消签名申请接口异常：" + inputDeductionStatconfirmorcancelResponse.getErrorResponse().getMessage());
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("false");
            // 具体的错误码
            ErrorResponse errorResponse = inputDeductionStatconfirmorcancelResponse.getErrorResponse();
            tInDeductionStatApply.setCode(errorResponse.getCode());
            tInDeductionStatApply.setMessage(errorResponse.getMessage());
            tInDeductionStatApply.setUpdateBy("接口返回异常");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改申请记录的结果
            tInDeductionStat.setSuccess("false");
            tInDeductionStat.setCode(errorResponse.getCode());
            tInDeductionStat.setMessage(errorResponse.getMessage());
            tInDeductionStat.setUpdateBy("取消签名异常");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
        }
        // 返回成功
        else {
            // 修改申请记录的结果
            tInDeductionStatApply.setSuccess("true");
            tInDeductionStatApply.setUpdateBy("接口返回成功");
            tInDeductionStatApplyService.updateTInDeductionStatApply(tInDeductionStatApply);
            // 修改统计表
            tInDeductionStat.setLastApplyTime(DateUtils.getNowDate());
            // 处理状态改为确认中，等待半小时后的查询结果
            tInDeductionStat.setStatFlowStatus(StatFlowStatus.QUE_REN_ZHONG.getCode());
            tInDeductionStat.setStatFlowStatusName(StatFlowStatus.QUE_REN_ZHONG.getName());
            tInDeductionStat.setUpdateBy("签名取消申请成功");
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
            // redis 插入 查询任务
            redisCache.setCacheObject(DeductionStatTask.STAT_CONFIRM_APPLY_RESULT_TASK_KEY, tInDeductionStat.getStatId());
            // 设置任务的执行时间（半小时）
            redisCache.setCacheObject(DeductionStatTask.STAT_CONFIRM_APPLY_RESULT_TASK_TTL_KEY, tInDeductionStat.getStatId(), 30, TimeUnit.MINUTES);
        }
    }


    /**
     * 同步统计表生成/取消结果
     * @param applyId
     */
    @Override
    public void syncDeductionStatApplyResultByApplyId(String applyId) {
        // 查询申请记录
        TInDeductionStatApply tInDeductionStatApply = tInDeductionStatApplyService.selectTInDeductionStatApplyByApplyId(applyId);
        // 生成一个新的结果对象
        TInDeductionStatApplyResult tInDeductionStatApplyResult = syncDeductionStatApplyResult(tInDeductionStatApply.getPeriod(), tInDeductionStatApply.getTaskType(), tInDeductionStatApply.getTaxNo());
        tInDeductionStatApplyResult.setApplyId(applyId);
        tInDeductionStatApplyResultService.updateTInDeductionStatApplyResult(tInDeductionStatApplyResult);
    }

    /**
     * 统计表状态同步
     * @param id
     */
    @Override
    public void syncDeductionStat(Long id) {
        TInDeductionStat tInDeductionStat = selectTInDeductionStatById(id);
        // 调用百望统计表结果查询接口
        InputDeductionGetstatistatusResponse inputDeductionGetstatistatusResponse = BaiwangUtils.deductionGetstatis(tInDeductionStat.getPeriod(), tInDeductionStat.getTaxNo());
        // 返回失败
        if(!inputDeductionGetstatistatusResponse.getSuccess()){
            logger.error("统计表生成/取消结果查询接口异常：" + inputDeductionGetstatistatusResponse.getErrorResponse().getMessage());
            throw new ServiceException(inputDeductionGetstatistatusResponse.getErrorResponse().getMessage());
        }
        // 返回成功
        else {
            InputDeductionGetstatistatus inputDeductionGetstatistatus = inputDeductionGetstatistatusResponse.getResponse();
            // 统计表状态
            if(!StringUtils.isEmpty(inputDeductionGetstatistatus.getStatstatus()) ){
                tInDeductionStat.setStatStatus(inputDeductionGetstatistatus.getStatstatus());
                tInDeductionStat.setStatStatusName(DictUtils.getDictLabel("stat_status", inputDeductionGetstatistatus.getStatstatus()));
            }
            // 处理状态
//            tInDeductionStat.setStatFlowStatus(getStatFlowStatusByApplyResultCode(inputDeductionStatapplyresult.getTaskType(), inputDeductionStatapplyresult.getResultCode()));
//            tInDeductionStat.setStatFlowStatusName(DictUtils.getDictLabel("stat_flow_status", tInDeductionStat.getStatFlowStatus()));
            // 指向该结果记录
            // tInDeductionStat.setResultId(tInDeductionStatApplyResult.getResultId());
            tInDeductionStat.setUpdateBy("同步结果成功");
            // 统计表明细
            List<TInDeductionStatEntry> tInDeductionStatEntryList = new ArrayList<>();
            for(InputDeductionGetstatistatusStatDetailInfo inputDeductionGetstatistatusStatDetailInfo : inputDeductionGetstatistatus.getStatList()){
                TInDeductionStatEntry tInDeductionStatEntry = new TInDeductionStatEntry();
                BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatEntry);
                BeanUtils.copyProperties(inputDeductionGetstatistatusStatDetailInfo, tInDeductionStatEntry);
                tInDeductionStatEntry.setInvoiceTypeName(DictUtils.getDictLabel("in_invoice_type", tInDeductionStatEntry.getInvoiceType()));
                if(StringUtils.isEmpty(tInDeductionStatEntry.getInvoiceTypeName())){
                    tInDeductionStatEntry.setInvoiceTypeName(DictUtils.getDictLabel("non_invoice_type", tInDeductionStatEntry.getInvoiceType()));
                }
                tInDeductionStatEntryList.add(tInDeductionStatEntry);
            }
            tInDeductionStat.setTInDeductionStatEntryList(tInDeductionStatEntryList);
            // 更新统计表
            updateTInDeductionStat(tInDeductionStat);
        }

        // 使用新的统计表结果查询接口

//        /*
//            根据统计表状态，调用 生成结果接口 或 确认结果结果
//         */
//        if(tInDeductionStat.getStatStatus().equals(StatStatus.STAT_NOT_GENERATED.getCode()) ||
//            tInDeductionStat.getStatStatus().equals(StatStatus.STAT_GENERATING.getCode()) ||
//            tInDeductionStat.getStatStatus().equals(StatStatus.STAT_GENERATED.getCode()) ||
//            tInDeductionStat.getStatStatus().equals(StatStatus.UNKNOWN.getCode())
//        ){
//            // 生成一个新的结果对象
//            syncDeductionStatApplyResult(tInDeductionStat.getPeriod(), TaskType.STATISTICS_TABLE_APPLICATION.getCode(), tInDeductionStat.getTaxNo());
//        }
//        if(
//            tInDeductionStat.getStatStatus().equals(StatStatus.CANCEL_STAT_IN_PROGRESS.getCode())
//        ){
//            // 生成一个新的结果对象
//            syncDeductionStatApplyResult(tInDeductionStat.getPeriod(), TaskType.STATISTICS_TABLE_CANCELLATION.getCode(), tInDeductionStat.getTaxNo());
//        }
//
//        if(
//            tInDeductionStat.getStatStatus().equals(StatStatus.CONFIRM_SIGNING_IN_PROGRESS.getCode()) ||
//            tInDeductionStat.getStatStatus().equals(StatStatus.CONFIRM_SIGN_SUCCESS.getCode())
//        ){
//            // 生成一个新的结果对象
//            syncDeductionStatConfirmApplyResult(tInDeductionStat.getPeriod(), ConfirmTaskType.APPLICATION.getCode(), tInDeductionStat.getTaxNo());
//        }
//
//        if(
//            tInDeductionStat.getStatStatus().equals(StatStatus.CANCEL_SIGN_IN_PROGRESS.getCode())
//        ){
//            // 生成一个新的结果对象
//            syncDeductionStatConfirmApplyResult(tInDeductionStat.getPeriod(), ConfirmTaskType.CANCELLATION.getCode(), tInDeductionStat.getTaxNo());
//        }
    }

    /**
     * 同步统计表生成/取消结果
     * @param period
     * @param taskType
     * @param buyerTaxNo
     */
    @Override
    public TInDeductionStatApplyResult syncDeductionStatApplyResult(String period, String taskType, String buyerTaxNo) {
        // 查询统计表
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByPeriod(period);
        // 生成一个新的结果对象
        TInDeductionStatApplyResult tInDeductionStatApplyResult = new TInDeductionStatApplyResult();
        BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatApplyResult);
        // 调用百望统计表生成/取消申请结果查询接口
        InputDeductionStatapplyresultResponse inputDeductionStatapplyresultResponse = BaiwangUtils.deductionStatApplyResult(period, taskType, buyerTaxNo);
        // 返回失败
        if(!inputDeductionStatapplyresultResponse.getSuccess()){
            logger.error("统计表生成/取消结果查询接口异常：" + inputDeductionStatapplyresultResponse.getErrorResponse().getMessage());
            // 修改结果
            tInDeductionStatApplyResult.setSuccess("false");
            // 具体的错误码
            ErrorResponse errorResponse = inputDeductionStatapplyresultResponse.getErrorResponse();
            tInDeductionStatApplyResult.setCode(errorResponse.getCode());
            tInDeductionStatApplyResult.setMessage(errorResponse.getMessage());
            tInDeductionStatApplyResult.setCreateBy("接口返回异常");
            tInDeductionStatApplyResultService.insertTInDeductionStatApplyResult(tInDeductionStatApplyResult);
            // 修改申请记录的结果
            tInDeductionStat.setSuccess("false");
            tInDeductionStat.setCode(errorResponse.getCode());
            tInDeductionStat.setMessage(errorResponse.getMessage());
            tInDeductionStat.setUpdateBy("生成/取消结果查询异常");
            // 更新统计表 仅主表
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
        }
        // 返回成功
        else {
            InputDeductionStatapplyresult inputDeductionStatapplyresult = inputDeductionStatapplyresultResponse.getResponse();
            // 修改申请记录的结果
            tInDeductionStatApplyResult.setSuccess("true");
            tInDeductionStatApplyResult.setResultCode(inputDeductionStatapplyresult.getResultCode());
            tInDeductionStatApplyResult.setResultName(DictUtils.getDictLabel("stat_result_code", inputDeductionStatapplyresult.getResultCode()));
            // 该接口不直接返回统计表状态，需要自行解析
            String statStatus = getStatStatusByApplyResultCode(inputDeductionStatapplyresult.getTaskType(), inputDeductionStatapplyresult.getResultCode());
            if(!StringUtils.isEmpty(statStatus)){
                tInDeductionStatApplyResult.setStatStatus(statStatus);
                tInDeductionStatApplyResult.setStatStatusName(DictUtils.getDictLabel("stat_status", statStatus));
            }
            tInDeductionStatApplyResult.setCreateBy("接口返回成功");
            // 结果明细
            List<TInDeductionStatApplyResultEntry> tInDeductionStatApplyResultEntryList = new ArrayList<>();
            for(InputDeductionStatapplyresultStatInfo deductionStatapplyresultStatInfo : inputDeductionStatapplyresult.getStatList()){
                TInDeductionStatApplyResultEntry tInDeductionStatApplyResultEntry = new TInDeductionStatApplyResultEntry();
                BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatApplyResultEntry);
                BeanUtils.copyProperties(deductionStatapplyresultStatInfo, tInDeductionStatApplyResultEntry);
                tInDeductionStatApplyResultEntryList.add(tInDeductionStatApplyResultEntry);
            }
            tInDeductionStatApplyResult.setTInDeductionStatApplyResultEntryList(tInDeductionStatApplyResultEntryList);
            tInDeductionStatApplyResultService.insertTInDeductionStatApplyResult(tInDeductionStatApplyResult);

            // 修改统计表结果码
            tInDeductionStat.setApplyResultCode(inputDeductionStatapplyresult.getResultCode());
            tInDeductionStat.setApplyResultName(DictUtils.getDictLabel("stat_result_code", inputDeductionStatapplyresult.getResultCode()));
            // 统计表状态
            tInDeductionStat.setStatStatus(tInDeductionStatApplyResult.getStatStatus());
            tInDeductionStat.setStatStatusName(tInDeductionStatApplyResult.getStatStatusName());
            // 处理状态
            tInDeductionStat.setStatFlowStatus(getStatFlowStatusByApplyResultCode(inputDeductionStatapplyresult.getTaskType(), inputDeductionStatapplyresult.getResultCode()));
            tInDeductionStat.setStatFlowStatusName(DictUtils.getDictLabel("stat_flow_status", tInDeductionStat.getStatFlowStatus()));
            // 指向该结果记录
            tInDeductionStat.setResultId(tInDeductionStatApplyResult.getResultId());
            tInDeductionStat.setUpdateBy("同步申请结果成功");
            // 统计表明细
            List<TInDeductionStatEntry> tInDeductionStatEntryList = new ArrayList<>();
            for(InputDeductionStatapplyresultStatInfo deductionStatapplyresultStatInfo : inputDeductionStatapplyresult.getStatList()){
                TInDeductionStatEntry tInDeductionStatEntry = new TInDeductionStatEntry();
                BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatEntry);
                BeanUtils.copyProperties(deductionStatapplyresultStatInfo, tInDeductionStatEntry);
                tInDeductionStatEntryList.add(tInDeductionStatEntry);
            }
            tInDeductionStat.setTInDeductionStatEntryList(tInDeductionStatEntryList);
            // 更新统计表
            updateTInDeductionStat(tInDeductionStat);
        }
        return tInDeductionStatApplyResult;
    }

    /**
     * 同步统计表签名确认/取消结果
     * @param applyId
     */
    @Override
    public void syncDeductionStatConfirmApplyResultByApplyId(String applyId) {
        // 查询申请记录
        TInDeductionStatApply tInDeductionStatApply = tInDeductionStatApplyService.selectTInDeductionStatApplyByApplyId(applyId);
        TInDeductionStatApplyResult tInDeductionStatApplyResult = syncDeductionStatConfirmApplyResult(tInDeductionStatApply.getPeriod(), tInDeductionStatApply.getTaskType(), tInDeductionStatApply.getTaxNo());
        tInDeductionStatApplyResult.setApplyId(applyId);
        tInDeductionStatApplyResultService.updateTInDeductionStatApplyResult(tInDeductionStatApplyResult);
    }

    /**
     * 同步统计表签名确认/取消结果
     * @param period
     * @param taskType
     * @param buyerTaxNo
     */
    @Override
    public TInDeductionStatApplyResult syncDeductionStatConfirmApplyResult(String period, String taskType, String buyerTaxNo) {
        // 查询统计表
        TInDeductionStat tInDeductionStat = selectTInDeductionStatByPeriod(period);
        // 生成一个新的结果对象
        TInDeductionStatApplyResult tInDeductionStatApplyResult = new TInDeductionStatApplyResult();
        BeanUtils.copyProperties(tInDeductionStat, tInDeductionStatApplyResult);

        // 调用百望统计表生成/取消申请结果查询接口
        InputDeductionStatconfirmorcancelresultResponse inputDeductionStatconfirmorcancelresultResponse = BaiwangUtils.deductionStatconfirmorcancelResult(period, taskType, buyerTaxNo);
        // 返回失败
        if(!inputDeductionStatconfirmorcancelresultResponse.getSuccess()){
            logger.error("统计表签名确认/取消结果查询接口异常：" + inputDeductionStatconfirmorcancelresultResponse.getErrorResponse().getMessage());
            // 修改结果
            tInDeductionStatApplyResult.setSuccess("false");
            // 具体的错误码
            ErrorResponse errorResponse = inputDeductionStatconfirmorcancelresultResponse.getErrorResponse();
            tInDeductionStatApplyResult.setCode(errorResponse.getCode());
            tInDeductionStatApplyResult.setMessage(errorResponse.getMessage());
            tInDeductionStatApplyResult.setCreateBy("接口返回异常");
            tInDeductionStatApplyResultService.insertTInDeductionStatApplyResult(tInDeductionStatApplyResult);
            // 修改申请记录的结果
            tInDeductionStat.setSuccess("false");
            tInDeductionStat.setCode(errorResponse.getCode());
            tInDeductionStat.setMessage(errorResponse.getMessage());
            tInDeductionStat.setUpdateBy("签名确认/取消结果查询异常");
            // 更新统计表 仅主表
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
        }
        // 返回成功
        else {
            InputDeductionStatconfirmorcancelresult inputDeductionStatconfirmorcancelresult = inputDeductionStatconfirmorcancelresultResponse.getResponse();
            // 修改申请记录的结果
            tInDeductionStatApplyResult.setSuccess("true");
            tInDeductionStatApplyResult.setResultCode(inputDeductionStatconfirmorcancelresult.getResultCode());
            tInDeductionStatApplyResult.setResultName(DictUtils.getDictLabel("confirm_result_code", inputDeductionStatconfirmorcancelresult.getResultCode()));
            // 该接口直接返回统计表状态
            tInDeductionStatApplyResult.setStatStatus(inputDeductionStatconfirmorcancelresult.getStatStatus());
            tInDeductionStatApplyResult.setStatStatusName(DictUtils.getDictLabel("stat_status", inputDeductionStatconfirmorcancelresult.getStatStatus()));
            tInDeductionStatApplyResult.setCreateBy("接口返回成功");
            // 该接口无结果明细返回
            tInDeductionStatApplyResultService.insertTInDeductionStatApplyResult(tInDeductionStatApplyResult);

            // 修改统计表签名确认结果码
            tInDeductionStat.setConfirmResultCode(inputDeductionStatconfirmorcancelresult.getResultCode());
            tInDeductionStat.setConfirmResultName(DictUtils.getDictLabel("confirm_result_code", inputDeductionStatconfirmorcancelresult.getResultCode()));
            // 统计表状态
            tInDeductionStat.setStatStatus(tInDeductionStatApplyResult.getStatStatus());
            tInDeductionStat.setStatStatusName(tInDeductionStatApplyResult.getStatStatusName());
            // 处理状态
            tInDeductionStat.setStatFlowStatus(getStatFlowStatusByApplyConfirmResultCode(inputDeductionStatconfirmorcancelresult.getTaskType(), inputDeductionStatconfirmorcancelresult.getResultCode()));
            tInDeductionStat.setStatFlowStatusName(DictUtils.getDictLabel("stat_flow_status", tInDeductionStat.getStatFlowStatus()));
            // 指向该结果记录
            tInDeductionStat.setResultId(tInDeductionStatApplyResult.getResultId());
            tInDeductionStat.setUpdateBy("同步确认结果成功");
            // 无统计表明细
            // 更新统计表 仅主表
            tInDeductionStatMapper.updateTInDeductionStat(tInDeductionStat);
        }
        return tInDeductionStatApplyResult;
    }

    /**
     * FIXME 此处转化状态逻辑有问题
     * 根据任务类型和返回结果码，返回统计表状态
     * 0-统计表未生成 1-统计表生成中 2-统计表已生成 4-确认签名中 5-确认签名成功 9-撤销统计表中 10-撤销签名中
     * @param taskType
     * @param resultCode
     * @return
     */
    private String getStatStatusByApplyResultCode(String taskType, String resultCode){

        if( resultCode.equals(StatResultCode.APPLICATION_MONTH_MISMATCH.getCode()) //5 申请统计月份不符
            || resultCode.equals(StatResultCode.NOT_MEET_CONDITION_FOR_STATISTICS.getCode()) //6 不符合申请生成抵扣统计报表条件
            || resultCode.equals(StatResultCode.NOT_STATISTICED_NO_NEED_CANCEL.getCode()) //7 未统计，不需要撤销
            || resultCode.equals(StatResultCode.DATE_RANGE_MISMATCH.getCode()) //9 不符合统计日期范围（每月一号到申报前）
            || resultCode.equals(StatResultCode.LOGIN_TAX_BUREAU_FAILED.getCode()) //13 登陆税局失败
            || resultCode.equals(StatResultCode.CHECK_TASK_EXECUTING.getCode()) //41 勾选任务执行中
            || resultCode.equals(StatResultCode.UNKNOWN.getCode()) //-1 未知
        ){
            // 返回 0 未生成
            return StatStatus.STAT_NOT_GENERATED.getCode();
        }
        else if(
            resultCode.equals(StatResultCode.PROCESSING.getCode()) // 1 申请成功
            || resultCode.equals(StatResultCode.REPEATED_APPLICATION.getCode()) // 2 重复申请统计
            || resultCode.equals(StatResultCode.ALREADY_STATISTICED.getCode()) // 3 已统计
            || resultCode.equals(StatResultCode.NOT_MEET_CANCELLATION_CONDITION.getCode()) // 10 不符合撤销申报条件 （已经申报结束，需要先撤销申报）
            || resultCode.equals(StatResultCode.CONFIRM_TASK_IN_PROGRESS.getCode()) // 43 确认任务中
        ){
            // 返回 2 统计表已生成
            return StatStatus.STAT_GENERATED.getCode();
        }
        else if (
            resultCode.equals(StatResultCode.ALREADY_CONFIRMED.getCode()) // 4 已确认
        ){
            // 返回 5
            return StatStatus.CONFIRM_SIGN_SUCCESS.getCode();
        }
        else if (
            resultCode.equals(StatResultCode.OTHER_ABNORMALITIES.getCode()) // 8 其他异常
        ){
            // 返回 -1
            return StatStatus.UNKNOWN.getCode();
        }
        else if (
            resultCode.equals(StatResultCode.PROCESSING.getCode()) // 0 处理中
        ){
            // 返回 统计表生成中
            return StatStatus.STAT_GENERATING.getCode();
        }
        return null;
    }

    /**
     * FIXME 此处转化状态逻辑有问题
     * 根据任务类型和返回结果码，返回统计表处理状态
     * 待申请统计表	0
     * 申请中	5
     * 已申请	10
     * 签名确认中	15
     * 已签名确认	20
     * @param taskType
     * @param resultCode
     * @return
     */
    private String getStatFlowStatusByApplyResultCode(String taskType, String resultCode){
        // 生成申请
        if(taskType.equals(TaskType.STATISTICS_TABLE_APPLICATION.getCode())){
            // 5 申请统计月份不符,
            // 6 不符合申请生成抵扣统计报表条件,
            // 7 未统计，不需要撤销,
            // 8 其他异常,
            // 9 不符合统计日期范围 （每月一号到申报前）,
            // 10 不符合撤销申报条件 （已经申报结束，需要先撤销申报）
            // 13 登陆税局失败
            // 41 勾选任务执行中
            // 43 确认任务中
            // -1未知
            if(resultCode.equals(StatResultCode.PROCESSING.getCode())){
                // 返回 待申请统计表	0
                return StatFlowStatus.SHEN_QING_ZHONG.getCode();
            }
            // 0 处理中, 2 重复申请统计,
            else if(resultCode.equals(StatResultCode.PROCESSING.getCode())){
                // 返回 申请中	5
                return StatStatus.STAT_GENERATING.getCode();
            }
            // 1 申请成功, 3 已统计,
            else if (resultCode.equals(StatResultCode.APPLICATION_SUCCESS.getCode())){
                // 返回 已申请	10
                return StatStatus.STAT_GENERATED.getCode();
            }
        } else {
            // 5 申请统计月份不符,
            // 6 不符合申请生成抵扣统计报表条件,
            // 7 未统计，不需要撤销,
            // 8 其他异常,
            // 9 不符合统计日期范围 （每月一号到申报前）,
            // 10 不符合撤销申报条件 （已经申报结束，需要先撤销申报）
            // 13 登陆税局失败
            // 41 勾选任务执行中
            // 43 确认任务中
            // -1未知
            if(resultCode.equals(StatResultCode.PROCESSING.getCode())){
                // 返回 待申请统计表	0
                return StatFlowStatus.SHEN_QING_ZHONG.getCode();
            }
            // 0 处理中, 2 重复申请统计,
            else if(resultCode.equals(StatResultCode.PROCESSING.getCode())){
                // 返回 申请中	5
                return StatStatus.STAT_GENERATING.getCode();
            }
            // 1 申请成功, 3 已统计,
            else if (resultCode.equals(StatResultCode.APPLICATION_SUCCESS.getCode())){
                // 返回 已申请	10
                return StatStatus.STAT_GENERATED.getCode();
            }
        }
        return StatStatus.UNKNOWN.getCode();
    }

    /**
     * FIXME 此处转化状态逻辑有问题
     * 根据任务类型和返回结果码，返回统计表处理状态
     * 待申请统计表	0
     * 申请中	5
     * 已申请	10
     * 签名确认中	15
     * 已签名确认	20
     * @param taskType
     * @param resultCode
     * @return
     */
    private String getStatFlowStatusByApplyConfirmResultCode(String taskType, String resultCode){
        // 确认签名申请
        if(taskType.equals(ConfirmTaskType.APPLICATION.getCode())){
            //    0 处理中
            //    1 申请成功
            //    2 已申请确认
            //    3 已确认统计
            //    4 申请确认月份不符
            //    5 税号不存在
            //    6未申请统计
            //    7 统计时间不符
            //    8 其他异常
            //    9 不符合提交确认申报条件（统计日期不在申报期内）
            //    10 未统计或者超期，不能撤销（是撤销签名时，未统计或者客户在税局手动取消签名）
            //    11 抵扣统计确认密码未设置或错误 
            //    13 登陆税局失败
            //    41 勾选任务执行中
            //    42 统计任务中
            // -1未知
            if(resultCode.equals(ConfirmResultCode.PROCESSING.getCode())){
                // 返回 待申请统计表	0
                return StatFlowStatus.SHEN_QING_ZHONG.getCode();
            }
            // 0 处理中, 2 重复申请统计,
            else if(resultCode.equals(ConfirmResultCode.PROCESSING.getCode())){
                // 返回 申请中	5
                return StatStatus.STAT_GENERATING.getCode();
            }
            // 1 申请成功, 3 已统计,
            else if (resultCode.equals(ConfirmResultCode.APPLICATION_SUCCESS.getCode())){
                // 返回 已申请	10
                return StatStatus.STAT_GENERATED.getCode();
            }
        } else {
            //    0 处理中
            //    1 申请成功
            //    2 已申请确认
            //    3 已确认统计
            //    4 申请确认月份不符
            //    5 税号不存在
            //    6未申请统计
            //    7 统计时间不符
            //    8 其他异常
            //    9 不符合提交确认申报条件（统计日期不在申报期内）
            //    10 未统计或者超期，不能撤销（是撤销签名时，未统计或者客户在税局手动取消签名）
            //    11 抵扣统计确认密码未设置或错误 
            //    13 登陆税局失败
            //    41 勾选任务执行中
            //    42 统计任务中
            if(resultCode.equals(ConfirmResultCode.PROCESSING.getCode())){
                // 返回 待申请统计表	0
                return StatFlowStatus.SHEN_QING_ZHONG.getCode();
            }
            // 0 处理中, 2 重复申请统计,
            else if(resultCode.equals(ConfirmResultCode.PROCESSING.getCode())){
                // 返回 申请中	5
                return StatStatus.STAT_GENERATING.getCode();
            }
            // 1 申请成功, 3 已统计,
            else if (resultCode.equals(ConfirmResultCode.APPLICATION_SUCCESS.getCode())){
                // 返回 已申请	10
                return StatStatus.STAT_GENERATED.getCode();
            }
        }
        return StatStatus.UNKNOWN.getCode();
    }
}
